home *** CD-ROM | disk | FTP | other *** search
/ Openstep 4.2 (Developer) / Openstep Developer 4.2.iso / NextDeveloper / Source / GNU / make / make-3.74 / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-11  |  56.0 KB  |  2,108 lines

  1. /* Argument parsing and main program of GNU Make.
  2. Copyright (C) 1988, 89, 90, 91, 94, 1995 Free Software Foundation, Inc.
  3. This file is part of GNU Make.
  4.  
  5. GNU Make is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. GNU Make is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU Make; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include "make.h"
  20. #include "commands.h"
  21. #include "dep.h"
  22. #include "file.h"
  23. #include "variable.h"
  24. #include "job.h"
  25. #include "getopt.h"
  26. #include <assert.h>
  27.  
  28.  
  29. extern void print_variable_data_base ();
  30. extern void print_dir_data_base ();
  31. extern void print_rule_data_base ();
  32. extern void print_file_data_base ();
  33. extern void print_vpath_data_base ();
  34.  
  35. #ifndef    HAVE_UNISTD_H
  36. extern int chdir ();
  37. #endif
  38. #ifndef    STDC_HEADERS
  39. #ifndef    sun            /* Sun has an incorrect decl in a header.  */
  40. extern void exit ();
  41. #endif
  42. extern double atof ();
  43. #endif
  44. extern char *mktemp ();
  45.  
  46. static void log_working_directory ();
  47. static void print_data_base (), print_version ();
  48. static void decode_switches (), decode_env_switches ();
  49. static void define_makeflags ();
  50. static char *quote_as_word ();
  51.  
  52. /* The structure that describes an accepted command switch.  */
  53.  
  54. #if NeXT || NeXT_PDO
  55. /*
  56.  * The NeXT and NeXT PDO implementations add some features by default.  They
  57.  * can be turned off selectively using the following flags:
  58.  *
  59.  *    -N all        Turn off all NeXT features.
  60.  *    -N caret    DON'T support the NeXT $> as synonym for $^
  61.  *    -N findfile    DON'T support the NeXT `findfile' directive.
  62.  *    -N makefiles    DON't support not remaking Makefiles  (remake them!)
  63.  *    -N quiet    DON'T be quiet: warn about using vpath compatibility
  64.  *            mode or missing targets or overriding implicit rules.
  65.  *    -N vpath    DON'T use the SYSV vpath compatibility mode.
  66.  *
  67.  *    OBSOLETE FLAGS:
  68.  *    -N noexport    Treat every makefile as though it contains the
  69.  *            target `.NOEXPORT'.
  70.  *    -N makevariable    Don't include flags in the definition of `MAKE'.
  71.  *    -N emptytarget    A missing target is not a fatal error.
  72.  *
  73.  */
  74. #endif
  75. struct command_switch
  76.   {
  77.     char c;            /* The switch character.  */
  78.  
  79.     enum            /* Type of the value.  */
  80.       {
  81.     flag,            /* Turn int flag on.  */
  82.     flag_off,        /* Turn int flag off.  */
  83.     string,            /* One string per switch.  */
  84.     positive_int,        /* A positive integer.  */
  85.     floating,        /* A floating-point number (double).  */
  86.     ignore            /* Ignored.  */
  87.       } type;
  88.  
  89.     char *value_ptr;    /* Pointer to the value-holding variable.  */
  90.  
  91.     unsigned int env:1;        /* Can come from MAKEFLAGS.  */
  92.     unsigned int toenv:1;    /* Should be put in MAKEFLAGS.  */
  93.     unsigned int no_makefile:1;    /* Don't propagate when remaking makefiles.  */
  94.  
  95.     char *noarg_value;    /* Pointer to value used if no argument is given.  */
  96.     char *default_value;/* Pointer to default value.  */
  97.  
  98.     char *long_name;        /* Long option name.  */
  99.     char *argdesc;        /* Descriptive word for argument.  */
  100.     char *description;        /* Description for usage message.  */
  101.   };
  102.  
  103.  
  104. /* The structure used to hold the list of strings given
  105.    in command switches of a type that takes string arguments.  */
  106.  
  107. struct stringlist
  108.   {
  109.     char **list;    /* Nil-terminated list of strings.  */
  110.     unsigned int idx;    /* Index into above.  */
  111.     unsigned int max;    /* Number of pointers allocated.  */
  112.   };
  113.  
  114.  
  115. /* The recognized command switches.  */
  116.  
  117. /* Nonzero means do not print commands to be executed (-s).  */
  118.  
  119. int silent_flag;
  120.  
  121. /* Nonzero means just touch the files
  122.    that would appear to need remaking (-t)  */
  123.  
  124. int touch_flag;
  125.  
  126. /* Nonzero means just print what commands would need to be executed,
  127.    don't actually execute them (-n).  */
  128.  
  129. int just_print_flag;
  130.  
  131. /* Print debugging trace info (-d).  */
  132.  
  133. int debug_flag = 0;
  134.  
  135. /* Environment variables override makefile definitions.  */
  136.  
  137. int env_overrides = 0;
  138.  
  139. /* Nonzero means ignore status codes returned by commands
  140.    executed to remake files.  Just treat them all as successful (-i).  */
  141.  
  142. int ignore_errors_flag = 0;
  143.  
  144. /* Nonzero means don't remake anything, just print the data base
  145.    that results from reading the makefile (-p).  */
  146.  
  147. int print_data_base_flag = 0;
  148.  
  149. /* Nonzero means don't remake anything; just return a nonzero status
  150.    if the specified targets are not up to date (-q).  */
  151.  
  152. int question_flag = 0;
  153.  
  154. /* Nonzero means do not use any of the builtin rules (-r).  */
  155.  
  156. int no_builtin_rules_flag = 0;
  157.  
  158. /* Nonzero means keep going even if remaking some file fails (-k).  */
  159.  
  160. int keep_going_flag;
  161. int default_keep_going_flag = 0;
  162.  
  163. /* Nonzero means print directory before starting and when done (-w).  */
  164.  
  165. #if NeXT || NeXT_PDO
  166. int print_directory_flag = 0;
  167. #else
  168. int print_directory_flag = 1;
  169. #endif
  170.  
  171. /* Nonzero means ignore print_directory_flag and never print the directory.
  172.    This is necessary because print_directory_flag is set implicitly.  */
  173.  
  174. int inhibit_print_directory_flag = 0;
  175.  
  176. /* Nonzero means print version information.  */
  177.  
  178. int print_version_flag = 0;
  179.  
  180. /* List of makefiles given with -f switches.  */
  181.  
  182. static struct stringlist *makefiles = 0;
  183.  
  184. #if NeXT || NeXT_PDO
  185. unsigned int next_flag = NEXT_DEFAULT_FLAGS;
  186. static struct stringlist *next_flag_list = 0;
  187. #endif
  188.  
  189. /* Number of job slots (commands that can be run at once).  */
  190.  
  191. unsigned int job_slots = 1;
  192. unsigned int default_job_slots = 1;
  193.  
  194. /* Value of job_slots that means no limit.  */
  195.  
  196. static unsigned int inf_jobs = 0;
  197.  
  198. /* Maximum load average at which multiple jobs will be run.
  199.    Negative values mean unlimited, while zero means limit to
  200.    zero load (which could be useful to start infinite jobs remotely
  201.    but one at a time locally).  */
  202.  
  203. double max_load_average = -1.0;
  204. double default_load_average = -1.0;
  205.  
  206. /* List of directories given with -C switches.  */
  207.  
  208. static struct stringlist *directories = 0;
  209.  
  210. /* List of include directories given with -I switches.  */
  211.  
  212. static struct stringlist *include_directories = 0;
  213.  
  214. /* List of files given with -o switches.  */
  215.  
  216. static struct stringlist *old_files = 0;
  217.  
  218. /* List of files given with -W switches.  */
  219.  
  220. static struct stringlist *new_files = 0;
  221.  
  222. /* If nonzero, we should just print usage and exit.  */
  223.  
  224. static int print_usage_flag = 0;
  225.  
  226. /* If nonzero, we should print a warning message
  227.    for each reference to an undefined variable.  */
  228.  
  229. int warn_undefined_variables_flag;
  230.  
  231. /* The table of command switches.  */
  232.  
  233. static const struct command_switch switches[] =
  234.   {
  235.     { 'b', ignore, 0, 0, 0, 0, 0, 0,
  236.     0, 0,
  237.     "Ignored for compatibility" },
  238.     { 'C', string, (char *) &directories, 0, 0, 0, 0, 0,
  239.     "directory", "DIRECTORY",
  240.     "Change to DIRECTORY before doing anything" },
  241.     { 'd', flag, (char *) &debug_flag, 1, 1, 0, 0, 0,
  242.     "debug", 0,
  243.     "Print lots of debugging information" },
  244.     { 'e', flag, (char *) &env_overrides, 1, 1, 0, 0, 0,
  245.     "environment-overrides", 0,
  246.     "Environment variables override makefiles" },
  247.     { 'f', string, (char *) &makefiles, 0, 0, 0, 0, 0,
  248.     "file", "FILE",
  249.     "Read FILE as a makefile" },
  250.     { 'h', flag, (char *) &print_usage_flag, 0, 0, 0, 0, 0,
  251.     "help", 0,
  252.     "Print this message and exit" },
  253.     { 'i', flag, (char *) &ignore_errors_flag, 1, 1, 0, 0, 0,
  254.     "ignore-errors", 0,
  255.     "Ignore errors from commands" },
  256.     { 'I', string, (char *) &include_directories, 1, 1, 0, 0, 0,
  257.     "include-dir", "DIRECTORY",
  258.     "Search DIRECTORY for included makefiles" },
  259.     { 'j', positive_int, (char *) &job_slots, 1, 1, 0,
  260.     (char *) &inf_jobs, (char *) &default_job_slots,
  261.     "jobs", "N",
  262.     "Allow N jobs at once; infinite jobs with no arg" },
  263.     { 'k', flag, (char *) &keep_going_flag, 1, 1, 0,
  264.     0, (char *) &default_keep_going_flag,
  265.     "keep-going", 0,
  266.     "Keep going when some targets can't be made" },
  267.     { 'l', floating, (char *) &max_load_average, 1, 1, 0,
  268.     (char *) &default_load_average, (char *) &default_load_average,
  269.     "load-average", "N",
  270.     "Don't start multiple jobs unless load is below N" },
  271.     { 'm', ignore, 0, 0, 0, 0, 0, 0,
  272.     0, 0,
  273.     "-b" },
  274.     { 'n', flag, (char *) &just_print_flag, 1, 1, 1, 0, 0,
  275.     "just-print", 0,
  276.     "Don't actually run any commands; just print them" },
  277.     { 'o', string, (char *) &old_files, 0, 0, 0, 0, 0,
  278.     "old-file", "FILE",
  279.     "Consider FILE to be very old and don't remake it" },
  280.     { 'p', flag, (char *) &print_data_base_flag, 1, 1, 0, 0, 0,
  281.     "print-data-base", 0,
  282.     "Print make's internal database" },
  283.     { 'q', flag, (char *) &question_flag, 1, 1, 1, 0, 0,
  284.     "question", 0,
  285.     "Run no commands; exit status says if up to date" },
  286.     { 'r', flag, (char *) &no_builtin_rules_flag, 1, 1, 0, 0, 0,
  287.     "no-builtin-rules", 0,
  288.     "Disable the built-in implicit rules" },
  289.     { 's', flag, (char *) &silent_flag, 1, 1, 0, 0, 0,
  290.     "silent", 0,
  291.     "Don't echo commands" },
  292.     { 'S', flag_off, (char *) &keep_going_flag, 1, 1, 0,
  293.     0, (char *) &default_keep_going_flag,
  294.     "no-keep-going", 0,
  295.     "Turns off -k" },
  296.     { 't', flag, (char *) &touch_flag, 1, 1, 1, 0, 0,
  297.     "touch", 0,
  298.     "Touch targets instead of remaking them" },
  299.     { 'v', flag, (char *) &print_version_flag, 1, 1, 0, 0, 0,
  300.     "version", 0,
  301.     "Print the version number of make and exit" },
  302.     { 'w', flag, (char *) &print_directory_flag, 1, 1, 0, 0, 0,
  303.     "print-directory", 0,
  304.     "Print the current directory" },
  305.     { 2, flag, (char *) &inhibit_print_directory_flag, 1, 1, 0, 0, 0,
  306.     "no-print-directory", 0,
  307.     "Turn off -w, even if it was turned on implicitly" },
  308.     { 'W', string, (char *) &new_files, 0, 0, 0, 0, 0,
  309.     "what-if", "FILE",
  310.     "Consider FILE to be infinitely new" },
  311. #if NeXT || NeXT_PDO
  312.     { 'N', string, (char *) &next_flag_list, 0, 0, 0, 0, 0,
  313.     "NeXT-option", "OPTION",
  314.     "Turn off value of NeXT OPTION" },
  315. #endif /* NeXT || NeXT_PDO */
  316.     { 3, flag, (char *) &warn_undefined_variables_flag, 1, 1, 0, 0, 0,
  317.     "warn-undefined-variables", 0,
  318.     "Warn when an undefined variable is referenced" },
  319.     { '\0', }
  320.   };
  321.  
  322. /* Secondary long names for options.  */
  323.  
  324. static struct option long_option_aliases[] =
  325.   {
  326.     { "quiet",        no_argument,        0, 's' },
  327.     { "stop",        no_argument,        0, 'S' },
  328.     { "new-file",    required_argument,    0, 'W' },
  329.     { "assume-new",    required_argument,    0, 'W' },
  330.     { "assume-old",    required_argument,    0, 'o' },
  331.     { "max-load",    optional_argument,    0, 'l' },
  332.     { "dry-run",    no_argument,        0, 'n' },
  333.     { "recon",        no_argument,        0, 'n' },
  334.     { "makefile",    required_argument,    0, 'f' },
  335.   };
  336.  
  337. /* The usage message prints the descriptions of options starting in
  338.    this column.  Make sure it leaves enough room for the longest
  339.    description to fit in less than 80 characters.  */
  340.  
  341. #define    DESCRIPTION_COLUMN    30
  342.  
  343. /* List of goal targets.  */
  344.  
  345. static struct dep *goals, *lastgoal;
  346.  
  347. /* List of variables which were defined on the command line
  348.    (or, equivalently, in MAKEFLAGS).  */
  349.  
  350. struct command_variable
  351.   {
  352.     struct command_variable *next;
  353.     struct variable *variable;
  354.   };
  355. static struct command_variable *command_variables;
  356.  
  357. /* The name we were invoked with.  */
  358.  
  359. char *program;
  360.  
  361. /* Our current directory before processing any -C options.  */
  362.  
  363. char *directory_before_chdir;
  364.  
  365. /* Our current directory after processing all -C options.  */
  366.  
  367. char *starting_directory;
  368.  
  369. /* Value of the MAKELEVEL variable at startup (or 0).  */
  370.  
  371. unsigned int makelevel;
  372.  
  373. /* First file defined in the makefile whose name does not
  374.    start with `.'.  This is the default to remake if the
  375.    command line does not specify.  */
  376.  
  377. struct file *default_goal_file;
  378.  
  379. /* Pointer to structure for the file .DEFAULT
  380.    whose commands are used for any file that has none of its own.
  381.    This is zero if the makefiles do not define .DEFAULT.  */
  382.  
  383. struct file *default_file;
  384.  
  385. /* Nonzero if we have seen the magic `.POSIX' target.
  386.    This turns on pedantic compliance with POSIX.2.  */
  387.  
  388. int posix_pedantic;
  389.  
  390. /* Mask of signals that are being caught with fatal_error_signal.  */
  391.  
  392. #ifdef    POSIX
  393. sigset_t fatal_signal_set;
  394. #else
  395. #ifdef    HAVE_SIGSETMASK
  396. int fatal_signal_mask;
  397. #endif
  398. #endif
  399.  
  400. static struct file *
  401. enter_command_line_file (name)
  402.      char *name;
  403. {
  404.   if (name[0] == '~')
  405.     {
  406.       char *expanded = tilde_expand (name);
  407.       if (expanded != 0)
  408.     name = expanded;    /* Memory leak; I don't care.  */
  409.     }
  410.  
  411.   /* This is also done in parse_file_seq, so this is redundant
  412.      for names read from makefiles.  It is here for names passed
  413.      on the command line.  */
  414.   while (name[0] == '.' && name[1] == '/' && name[2] != '\0')
  415.     {
  416.       name += 2;
  417.       while (*name == '/')
  418.     /* Skip following slashes: ".//foo" is "foo", not "/foo".  */
  419.     ++name;
  420.     }
  421.   
  422.   if (*name == '\0')
  423.     {
  424.       /* It was all slashes!  Move back to the dot and truncate
  425.      it after the first slash, so it becomes just "./".  */
  426.       do
  427.     --name;
  428.       while (name[0] != '.');
  429.       name[2] = '\0';
  430.     }
  431.  
  432.   return enter_file (savestring (name, strlen (name)));
  433. }
  434.  
  435. /* Toggle -d on receipt of SIGUSR1.  */
  436.  
  437. static RETSIGTYPE
  438. debug_signal_handler (sig)
  439.      int sig;
  440. {
  441.   debug_flag = ! debug_flag;
  442. }
  443.  
  444. #ifdef DEBUG
  445. #ifdef WIN32
  446. #include <ntunix.h>
  447. #else
  448. #include <sys/time.h>
  449. #endif
  450. struct timeval start_time, end_time;
  451. struct timezone time_zone;
  452. #endif
  453.  
  454. int
  455. main (argc, argv, envp)
  456.      int argc;
  457.      char **argv;
  458.      char **envp;
  459. {
  460.   extern void init_dir ();
  461.   extern RETSIGTYPE fatal_error_signal (), child_handler ();
  462.   register struct file *f;
  463.   register unsigned int i;
  464.   char **p;
  465.   struct dep *read_makefiles;
  466.   PATH_VAR (current_directory);
  467. #ifdef DEBUG
  468.   gettimeofday(&start_time, &time_zone);
  469. #endif
  470.      
  471.   default_goal_file = 0;
  472.   reading_filename = 0;
  473.   reading_lineno_ptr = 0;
  474.   
  475. #if !defined (HAVE_STRSIGNAL) && !defined (HAVE_SYS_SIGLIST)
  476.   signame_init ();
  477. #endif
  478.  
  479. #ifdef    POSIX
  480.   sigemptyset (&fatal_signal_set);
  481. #define    ADD_SIG(sig)    sigaddset (&fatal_signal_set, sig)
  482. #else
  483. #ifdef    HAVE_SIGSETMASK
  484.   fatal_signal_mask = 0;
  485. #define    ADD_SIG(sig)    fatal_signal_mask |= sigmask (sig)
  486. #else
  487. #define    ADD_SIG(sig)
  488. #endif
  489. #endif
  490.  
  491. #define    FATAL_SIG(sig)                                  \
  492.   if (signal ((sig), fatal_error_signal) == SIG_IGN)                  \
  493.     (void) signal ((sig), SIG_IGN);                          \
  494.   else                                          \
  495.     ADD_SIG (sig);
  496.  
  497. #ifndef WIN32
  498.   FATAL_SIG (SIGHUP);
  499.   FATAL_SIG (SIGQUIT);
  500. #endif
  501.   FATAL_SIG (SIGINT);
  502.   FATAL_SIG (SIGTERM);
  503.  
  504. #ifdef    SIGDANGER
  505.   FATAL_SIG (SIGDANGER);
  506. #endif
  507. #ifdef SIGXCPU
  508.   FATAL_SIG (SIGXCPU);
  509. #endif
  510. #ifdef SIGXFSZ
  511.   FATAL_SIG (SIGXFSZ);
  512. #endif
  513.  
  514. #undef    FATAL_SIG
  515.  
  516.   /* Make sure stdout is line-buffered.  */
  517.  
  518. #ifdef    HAVE_SETLINEBUF
  519.   setlinebuf (stdout);
  520. #else
  521. #ifndef    SETVBUF_REVERSED
  522.   setvbuf (stdout, (char *) 0, _IOLBF, BUFSIZ);
  523. #else    /* setvbuf not reversed.  */
  524.   /* Some buggy systems lose if we pass 0 instead of allocating ourselves.  */
  525.   setvbuf (stdout, _IOLBF, xmalloc (BUFSIZ), BUFSIZ);
  526. #endif    /* setvbuf reversed.  */
  527. #endif    /* setlinebuf missing.  */
  528.  
  529.   /* Initialize the directory hashing code.  */
  530.   init_dir ();
  531.  
  532.   /* Figure out where this program lives.  */
  533.  
  534.   if (argv[0] == 0)
  535.     argv[0] = "";
  536.   if (argv[0][0] == '\0')
  537.     program = "make";
  538.   else 
  539.     {
  540.       program = rindex (argv[0], '/');
  541. #if defined (__MSDOS__) || defined (WIN32)
  542.       if (program == 0)
  543.     program = rindex (argv[0], '\\');
  544.       if (program == 0)
  545.     program = rindex (argv[0], ':');
  546. #endif
  547.       if (program == 0)
  548.     program = argv[0];
  549.       else
  550.     ++program;
  551.     }
  552.  
  553.   /* Set up to access user data (files).  */
  554.   user_access ();
  555.  
  556.   /* Figure out where we are.  */
  557.  
  558.   if (getcwd (current_directory, GET_PATH_MAX) == 0)
  559.     {
  560. #ifdef    HAVE_GETCWD
  561.       perror_with_name ("getcwd: ", "");
  562. #else
  563.       error ("getwd: %s", current_directory);
  564. #endif
  565.       current_directory[0] = '\0';
  566.       directory_before_chdir = 0;
  567.     }
  568.   else
  569.     directory_before_chdir = savestring (current_directory,
  570.                      strlen (current_directory));
  571.  
  572.   /* Read in variables from the environment.  It is important that this be
  573.      done before $(MAKE) is are figured out so its definitions will not be
  574.      one from the environment.  */
  575.  
  576.   for (i = 0; envp[i] != 0; ++i)
  577.     {
  578.       register char *ep = envp[i];
  579.       while (*ep != '=')
  580.     ++ep;
  581.       /* The result of pointer arithmetic is cast to unsigned int for
  582.      machines where ptrdiff_t is a different size that doesn't widen
  583.      the same.  */
  584.       define_variable (envp[i], (unsigned int) (ep - envp[i]),
  585.                ep + 1, o_env, 1)
  586.     /* Force exportation of every variable culled from the environment.
  587.        We used to rely on target_environment's v_default code to do this.
  588.        But that does not work for the case where an environment variable
  589.        is redefined in a makefile with `override'; it should then still
  590.        be exported, because it was originally in the environment.  */
  591.     ->export = v_export;
  592.     }
  593.  
  594.   /* Decode the switches.  */
  595.  
  596.   decode_env_switches ("MAKEFLAGS", 9);
  597. #if 0
  598.   /* People write things like:
  599.          MFLAGS="CC=gcc -pipe" "CFLAGS=-g"
  600.      and we set the -p, -i and -e switches.  Doesn't seem quite right.  */
  601.   decode_env_switches ("MFLAGS", 6);
  602. #endif
  603.   decode_switches (argc, argv, 0);
  604.  
  605. #if NeXT || NeXT_PDO
  606.   if (next_flag_list != 0) {
  607.         char **p;
  608.       next_flag = NEXT_DEFAULT_FLAGS;
  609.       for (p = next_flag_list->list; *p != 0; p++) {
  610.       if (strcmp(*p, "vpath") == 0) {
  611.           next_flag &= ~NEXT_VPATH_FLAG;
  612. #if 0
  613.           /* these are built into the 3.69 version */
  614.       } else if (strcmp(*p, "noexport") == 0) {
  615.           next_flag &= ~NEXT_NOEXPORT_FLAG;
  616.       } else if (strcmp(*p, "emptytarget") == 0) {
  617.           next_flag &= ~NEXT_EMPTYTARGET_FLAG;
  618.       } else if (strcmp(*p, "makevariable") == 0) {
  619.           next_flag &= ~NEXT_MAKEVARIABLE_FLAG;
  620. #endif
  621.       } else if (strcmp(*p, "quiet") == 0) {
  622.           next_flag &= ~NEXT_QUIET_FLAG;
  623.       } else if (strcmp(*p, "makefiles") == 0) {
  624.           next_flag &= ~NEXT_MAKEFILES_FLAG;
  625.       } else if (strcmp(*p, "findfile") == 0) {
  626.           next_flag &= ~NEXT_FINDFILE_FLAG;
  627.       } else if (strcmp(*p, "caret") == 0) {
  628.           next_flag &= ~NEXT_CARET_FLAG;
  629.       } else if (strcmp(*p, "all") == 0) {
  630.           next_flag = 0;
  631.       } else {
  632.           error ("Unrecognized flag `%s'.", *p);
  633.       }
  634.       }
  635.   }
  636. #endif    /* NeXT || NeXT_PDO */
  637.  
  638.   /* Print version information.  */
  639.  
  640.   if (print_version_flag || print_data_base_flag || debug_flag)
  641.     print_version ();
  642.  
  643.   /* `make --version' is supposed to just print the version and exit.  */
  644.   if (print_version_flag)
  645.     die (0);
  646.  
  647. #if ! (defined (__MSDOS__) || defined (WIN32))
  648.   /* Set the "MAKE_COMMAND" variable to the name we were invoked with.
  649.      (If it is a relative pathname with a slash, prepend our directory name
  650.      so the result will run the same program regardless of the current dir.
  651.      If it is a name with no slash, we can only hope that PATH did not
  652.      find it in the current directory.)  */
  653.  
  654.   if (current_directory[0] != '\0'
  655.       && argv[0] != 0 && argv[0][0] != '/' && index (argv[0], '/') != 0)
  656.     argv[0] = concat (current_directory, "/", argv[0]);
  657. #endif
  658.  
  659. #if defined (__MSDOS__) || defined (WIN32)
  660.   /* ensure that argv[0] contains only / characters */
  661.   for (i=0; argv[0][i]; i++)
  662.     if (argv[0][i] == '\\') argv[0][i] = '/';
  663. #endif
  664.  
  665.   /* The extra indirection through $(MAKE_COMMAND) is done
  666.      for hysterical raisins.  */
  667.   (void) define_variable ("MAKE_COMMAND", 12, argv[0], o_default, 0);
  668.   (void) define_variable ("MAKE", 4, "$(MAKE_COMMAND)", o_default, 1);
  669.  
  670.   if (command_variables != 0)
  671.     {
  672.       struct command_variable *cv;
  673.       struct variable *v;
  674.       unsigned int len = 0;
  675.       char *value, *p;
  676.  
  677.       /* Figure out how much space will be taken up by the command-line
  678.      variable definitions.  */
  679.       for (cv = command_variables; cv != 0; cv = cv->next)
  680.     {
  681.       v = cv->variable;
  682.       len += 2 * strlen (v->name);
  683.       if (! v->recursive)
  684.         ++len;
  685.       ++len;
  686.       len += 2 * strlen (v->value);
  687.     }
  688.  
  689.       /* Now allocate a buffer big enough and fill it.  */
  690.       p = value = (char *) alloca (len);
  691.       for (cv = command_variables; cv != 0; cv = cv->next)
  692.     {
  693.       v = cv->variable;
  694.       p = quote_as_word (p, v->name, 0);
  695.       if (! v->recursive)
  696.         *p++ = ':';
  697.       *p++ = '=';
  698.       p = quote_as_word (p, v->value, 0);
  699.       *p++ = ' ';
  700.     }
  701.       p[-1] = '\0';        /* Kill the final space and terminate.  */
  702.  
  703.       /* Define an unchangeable variable with a name that no POSIX.2
  704.      makefile could validly use for its own variable.  */
  705.       (void) define_variable ("-*-command-variables-*-", 23,
  706.                   value, o_automatic, 0);
  707.  
  708.       /* Define the variable; this will not override any user definition.
  709.          Normally a reference to this variable is written into the value of
  710.          MAKEFLAGS, allowing the user to override this value to affect the
  711.          exported value of MAKEFLAGS.  In POSIX-pedantic mode, we cannot
  712.          allow the user's setting of MAKEOVERRIDES to affect MAKEFLAGS, so
  713.          a reference to this hidden variable is written instead. */
  714.       (void) define_variable ("MAKEOVERRIDES", 13,
  715.                   "${-*-command-variables-*-}", o_env, 1);
  716.     }
  717.  
  718.   /* If there were -C flags, move ourselves about.  */
  719.   if (directories != 0)
  720.     for (i = 0; directories->list[i] != 0; ++i)
  721.       {
  722.     char *dir = directories->list[i];
  723.     if (dir[0] == '~')
  724.       {
  725.         char *expanded = tilde_expand (dir);
  726.         if (expanded != 0)
  727.           dir = expanded;
  728.       }
  729.     if (chdir (dir) < 0)
  730.       pfatal_with_name (dir);
  731.     if (dir != directories->list[i])
  732.       free (dir);
  733.       }
  734.  
  735.   /* Figure out the level of recursion.  */
  736.   {
  737.     struct variable *v = lookup_variable ("MAKELEVEL", 9);
  738.     if (v != 0 && *v->value != '\0' && *v->value != '-')
  739.       makelevel = (unsigned int) atoi (v->value);
  740.     else
  741.       makelevel = 0;
  742.   }
  743.  
  744. #if ! (NeXT || NeXT_PDO)
  745.   /* Except under -s, always do -w in sub-makes and under -C.  */
  746.   if (!silent_flag && (directories != 0 || makelevel > 0))
  747.     print_directory_flag = 1;
  748. #endif
  749.  
  750.   /* Let the user disable that with --no-print-directory.  */
  751.   if (inhibit_print_directory_flag)
  752.     print_directory_flag = 0;
  753.  
  754.   /* Construct the list of include directories to search.  */
  755.  
  756.   construct_include_path (include_directories == 0 ? (char **) 0
  757.               : include_directories->list);
  758.  
  759.   /* Figure out where we are now, after chdir'ing.  */
  760.   if (directories == 0)
  761.     /* We didn't move, so we're still in the same place.  */
  762.     starting_directory = current_directory;
  763.   else
  764.     {
  765.       if (getcwd (current_directory, GET_PATH_MAX) == 0)
  766.     {
  767. #ifdef    HAVE_GETCWD
  768.       perror_with_name ("getcwd: ", "");
  769. #else
  770.       error ("getwd: %s", current_directory);
  771. #endif
  772.       starting_directory = 0;
  773.     }
  774.       else
  775.     starting_directory = current_directory;
  776.     }
  777.  
  778.   /* Tell the user where he is.  */
  779.  
  780.   if (print_directory_flag)
  781.     log_working_directory (1);
  782.  
  783.   /* Read any stdin makefiles into temporary files.  */
  784.  
  785.   if (makefiles != 0)
  786.     {
  787.       register unsigned int i;
  788.       for (i = 0; i < makefiles->idx; ++i)
  789.     if (makefiles->list[i][0] == '-' && makefiles->list[i][1] == '\0')
  790.       {
  791.         /* This makefile is standard input.  Since we may re-exec
  792.            and thus re-read the makefiles, we read standard input
  793.            into a temporary file and read from that.  */
  794.         FILE *outfile;
  795.  
  796.         /* Make a unique filename.  */
  797. #ifdef HAVE_MKTEMP
  798.         static char name[] = "/tmp/GmXXXXXX";
  799.         (void) mktemp (name);
  800. #else
  801.         static char name[L_tmpnam];
  802.         (void) tmpnam (name);
  803. #endif
  804.  
  805.         outfile = fopen (name, "w");
  806.         if (outfile == 0)
  807.           pfatal_with_name ("fopen (temporary file)");
  808.         while (!feof (stdin))
  809.           {
  810.         char buf[2048];
  811.         int n = fread (buf, 1, sizeof(buf), stdin);
  812.         if (n > 0 && fwrite (buf, 1, n, outfile) != n)
  813.           pfatal_with_name ("fwrite (temporary file)");
  814.           }
  815.         /* Try to make sure we won't remake the temporary
  816.            file when we are re-exec'd.  Kludge-o-matic!  */
  817.         fprintf (outfile, "%s:;\n", name);
  818.         (void) fclose (outfile);
  819.  
  820.         /* Replace the name that read_all_makefiles will
  821.            see with the name of the temporary file.  */
  822.         {
  823.           char *temp;
  824.           /* SGI compiler requires alloca's result be assigned simply.  */
  825.           temp = (char *) alloca (sizeof (name));
  826.           bcopy (name, temp, sizeof (name));
  827.           makefiles->list[i] = temp;
  828.         }
  829.  
  830.         /* Make sure the temporary file will not be remade.  */
  831.         f = enter_file (savestring (name, sizeof name - 1));
  832.         f->updated = 1;
  833.         f->update_status = 0;
  834.         f->command_state = cs_finished;
  835.         /* Let it be removed when we're done.  */
  836.         f->intermediate = 1;
  837.         /* But don't mention it.  */
  838.         f->dontcare = 1;
  839.       }
  840.     }
  841.  
  842.   /* Set up to handle children dying.  This must be done before
  843.      reading in the makefiles so that `shell' function calls will work.  */
  844.  
  845. #ifdef SIGCHLD
  846.   (void) signal (SIGCHLD, child_handler);
  847. #endif
  848. #ifdef SIGCLD
  849.   (void) signal (SIGCLD, child_handler);
  850. #endif
  851.  
  852.   /* Let the user send us SIGUSR1 to toggle the -d flag during the run.  */
  853. #ifdef SIGUSR1
  854.   (void) signal (SIGUSR1, debug_signal_handler);
  855. #endif
  856.  
  857.   /* Define the initial list of suffixes for old-style rules.  */
  858.  
  859.   set_default_suffixes ();
  860.  
  861.   /* Define the file rules for the built-in suffix rules.  These will later
  862.      be converted into pattern rules.  We used to do this in
  863.      install_default_implicit_rules, but since that happens after reading
  864.      makefiles, it results in the built-in pattern rules taking precedence
  865.      over makefile-specified suffix rules, which is wrong.  */
  866.  
  867.   install_default_suffix_rules ();
  868.  
  869.   /* Define some internal and special variables.  */
  870.  
  871.   define_automatic_variables ();
  872.  
  873.   /* Set up the MAKEFLAGS and MFLAGS variables
  874.      so makefiles can look at them.  */
  875.  
  876.   define_makeflags (0, 0);
  877.  
  878.   /* Define the default variables.  */
  879.   define_default_variables ();
  880.  
  881.   /* Read all the makefiles.  */
  882.  
  883.   default_file = enter_file (".DEFAULT");
  884.  
  885.   read_makefiles
  886.     = read_all_makefiles (makefiles == 0 ? (char **) 0 : makefiles->list);
  887.  
  888. #ifdef DEBUG
  889.         gettimeofday(&end_time, &time_zone);
  890.         end_time.tv_sec -= start_time.tv_sec;
  891.         end_time.tv_usec -= start_time.tv_usec;
  892.         fprintf(stderr,"gnumake[%d]: *** Done reading Makefiles in %3d.%01d (seconds real).\n",
  893.                 getpid(), end_time.tv_sec,
  894.                 (int) (end_time.tv_usec/100000));
  895.         fflush(stderr);
  896. #endif
  897.  
  898.   /* Decode switches again, in case the variables were set by the makefile.  */
  899.   decode_env_switches ("MAKEFLAGS", 9);
  900. #if 0
  901.   decode_env_switches ("MFLAGS", 6);
  902. #endif
  903.  
  904.   /* Set up MAKEFLAGS and MFLAGS again, so they will be right.  */
  905.  
  906.   define_makeflags (1, 0);
  907.  
  908.   /* Make each `struct dep' point at the `struct file' for the file
  909.      depended on.  Also do magic for special targets.  */
  910.  
  911.   snap_deps ();
  912.  
  913.   /* Convert old-style suffix rules to pattern rules.  It is important to
  914.      do this before installing the built-in pattern rules below, so that
  915.      makefile-specified suffix rules take precedence over built-in pattern
  916.      rules.  */
  917.  
  918.   convert_to_pattern ();
  919.  
  920.   /* Install the default implicit pattern rules.
  921.      This used to be done before reading the makefiles.
  922.      But in that case, built-in pattern rules were in the chain
  923.      before user-defined ones, so they matched first.  */
  924.  
  925.   install_default_implicit_rules ();
  926.  
  927.   /* Compute implicit rule limits.  */
  928.  
  929.   count_implicit_rule_limits ();
  930.  
  931.   /* Construct the listings of directories in VPATH lists.  */
  932.  
  933.   build_vpath_lists ();
  934.  
  935.   /* Mark files given with -o flags as very old (00:00:01.00 Jan 1, 1970)
  936.      and as having been updated already, and files given with -W flags as
  937.      brand new (time-stamp as far as possible into the future).  */
  938.  
  939.   if (old_files != 0)
  940.     for (p = old_files->list; *p != 0; ++p)
  941.       {
  942.     f = enter_command_line_file (*p);
  943.     f->last_mtime = (time_t) 1;
  944.     f->updated = 1;
  945.     f->update_status = 0;
  946.     f->command_state = cs_finished;
  947.       }
  948.  
  949.   if (new_files != 0)
  950.     {
  951.       for (p = new_files->list; *p != 0; ++p)
  952.     {
  953.       f = enter_command_line_file (*p);
  954.       f->last_mtime = NEW_MTIME;
  955.     }
  956.     }
  957.  
  958.   if (read_makefiles != 0)
  959. #if NeXT || NeXT_PDO
  960.       if (!(next_flag & NEXT_MAKEFILES_FLAG))
  961. #endif
  962.     {
  963.       /* Update any makefiles if necessary.  */
  964.  
  965.       time_t *makefile_mtimes = 0;
  966.       unsigned int mm_idx = 0;
  967.  
  968.       if (debug_flag)
  969.     puts ("Updating makefiles....");
  970.  
  971.       /* Remove any makefiles we don't want to try to update.
  972.      Also record the current modtimes so we can compare them later.  */
  973.       {
  974.     register struct dep *d, *last;
  975.     last = 0;
  976.     d = read_makefiles;
  977.     while (d != 0)
  978.       {
  979.         register struct file *f = d->file;
  980.         if (f->double_colon)
  981.           for (f = f->double_colon; f != NULL; f = f->prev)
  982.         {
  983.           if (f->deps == 0 && f->cmds != 0)
  984.             {
  985.               /* This makefile is a :: target with commands, but
  986.              no dependencies.  So, it will always be remade.
  987.              This might well cause an infinite loop, so don't
  988.              try to remake it.  (This will only happen if
  989.              your makefiles are written exceptionally
  990.              stupidly; but if you work for Athena, that's how
  991.              you write your makefiles.)  */
  992.  
  993.               if (debug_flag)
  994.             printf ("Makefile `%s' might loop; not remaking it.\n",
  995.                 f->name);
  996.  
  997.               if (last == 0)
  998.             read_makefiles = d->next;
  999.               else
  1000.             last->next = d->next;
  1001.  
  1002.               /* Free the storage.  */
  1003.               free ((char *) d);
  1004.  
  1005.               d = last == 0 ? 0 : last->next;
  1006.  
  1007.               break;
  1008.             }
  1009.         }
  1010.         if (f == NULL || !f->double_colon)
  1011.           {
  1012.         if (makefile_mtimes == 0)
  1013.           makefile_mtimes = (time_t *) xmalloc (sizeof (time_t));
  1014.         else
  1015.           makefile_mtimes = (time_t *)
  1016.             xrealloc ((char *) makefile_mtimes,
  1017.                   (mm_idx + 1) * sizeof (time_t));
  1018.         makefile_mtimes[mm_idx++] = file_mtime_no_search (d->file);
  1019.         last = d;
  1020.         d = d->next;
  1021.           }
  1022.       }
  1023.       }    
  1024.  
  1025.       /* Set up `MAKEFLAGS' specially while remaking makefiles.  */
  1026.       define_makeflags (1, 1);
  1027.  
  1028.       switch (update_goal_chain (read_makefiles, 1))
  1029.     {
  1030.     case 1:
  1031.     default:
  1032. #define BOGUS_UPDATE_STATUS 0
  1033.       assert (BOGUS_UPDATE_STATUS);
  1034.       break;
  1035.       
  1036.     case -1:
  1037.       /* Did nothing.  */
  1038.       break;
  1039.       
  1040.     case 2:
  1041.       /* Failed to update.  Figure out if we care.  */
  1042.       {
  1043.         /* Nonzero if any makefile was successfully remade.  */
  1044.         int any_remade = 0;
  1045.         /* Nonzero if any makefile we care about failed
  1046.            in updating or could not be found at all.  */
  1047.         int any_failed = 0;
  1048.         register unsigned int i;
  1049.  
  1050.         for (i = 0; read_makefiles != 0; ++i)
  1051.           {
  1052.         struct dep *d = read_makefiles;
  1053.         read_makefiles = d->next;
  1054.         if (d->file->updated)
  1055.           {
  1056.             /* This makefile was updated.  */
  1057.             if (d->file->update_status == 0)
  1058.               {
  1059.             /* It was successfully updated.  */
  1060.             any_remade |= (file_mtime_no_search (d->file)
  1061.                        != makefile_mtimes[i]);
  1062.               }
  1063.             else if (! (d->changed & RM_DONTCARE))
  1064.               {
  1065.             time_t mtime;
  1066.             /* The update failed and this makefile was not
  1067.                from the MAKEFILES variable, so we care.  */
  1068.             error ("Failed to remake makefile `%s'.",
  1069.                    d->file->name);
  1070.             mtime = file_mtime_no_search (d->file);
  1071.             any_remade |= (mtime != (time_t) -1
  1072.                        && mtime != makefile_mtimes[i]);
  1073.               }
  1074.           }
  1075.         else
  1076.           /* This makefile was not found at all.  */
  1077.           if (! (d->changed & RM_DONTCARE))
  1078.             {
  1079.               /* This is a makefile we care about.  See how much.  */
  1080.               if (d->changed & RM_INCLUDED)
  1081.             /* An included makefile.  We don't need
  1082.                to die, but we do want to complain.  */
  1083.             error ("Included makefile `%s' was not found.",
  1084.                    dep_name (d));
  1085.               else
  1086.             {
  1087.               /* A normal makefile.  We must die later.  */
  1088.               error ("Makefile `%s' was not found", dep_name (d));
  1089.               any_failed = 1;
  1090.             }
  1091.             }
  1092.  
  1093.         free ((char *) d);
  1094.           }
  1095.  
  1096.         if (any_remade)
  1097.           goto re_exec;
  1098.         else if (any_failed)
  1099.           die (2);
  1100.         else
  1101.           break;
  1102.       }
  1103.  
  1104.     case 0:
  1105.     re_exec:
  1106.       /* Updated successfully.  Re-exec ourselves.  */
  1107.  
  1108.       remove_intermediates (0);
  1109.  
  1110.       if (print_data_base_flag)
  1111.         print_data_base ();
  1112.  
  1113.       if (print_directory_flag)
  1114.         log_working_directory (0);
  1115.  
  1116.       if (makefiles != 0)
  1117.         {
  1118.           /* These names might have changed.  */
  1119.           register unsigned int i, j = 0;
  1120.           for (i = 1; i < argc; ++i)
  1121.         if (!strcmp (argv[i], "-f")) /* XXX */
  1122.           {
  1123.             char *p = &argv[i][2];
  1124.             if (*p == '\0')
  1125.               argv[++i] = makefiles->list[j];
  1126.             else
  1127.               argv[i] = concat ("-f", makefiles->list[j], "");
  1128.             ++j;
  1129.           }
  1130.         }
  1131.  
  1132.       if (directories != 0 && directories->idx > 0)
  1133.         {
  1134.           char bad;
  1135.           if (directory_before_chdir != 0)
  1136.         {
  1137.           if (chdir (directory_before_chdir) < 0)
  1138.             {
  1139.               perror_with_name ("chdir", "");
  1140.               bad = 1;
  1141.             }
  1142.           else
  1143.             bad = 0;
  1144.         }
  1145.           else
  1146.         bad = 1;
  1147.           if (bad)
  1148.         fatal ("Couldn't change back to original directory.");
  1149.         }
  1150.  
  1151.       for (p = environ; *p != 0; ++p)
  1152.         if (!strncmp (*p, "MAKELEVEL=", 10))
  1153.           {
  1154.         /* The SGI compiler apparently can't understand
  1155.            the concept of storing the result of a function
  1156.            in something other than a local variable.  */
  1157.         char *sgi_loses;
  1158.         sgi_loses = (char *) alloca (40);
  1159.         *p = sgi_loses;
  1160.         sprintf (*p, "MAKELEVEL=%u", makelevel);
  1161.         break;
  1162.           }
  1163.  
  1164.       if (debug_flag)
  1165.         {
  1166.           char **p;
  1167.           fputs ("Re-executing:", stdout);
  1168.           for (p = argv; *p != 0; ++p)
  1169.         printf (" %s", *p);
  1170.           puts ("");
  1171.         }
  1172.  
  1173.       fflush (stdout);
  1174.       fflush (stderr);
  1175.  
  1176.       exec_command (argv, environ);
  1177.       /* NOTREACHED */
  1178.     }
  1179.     }
  1180.  
  1181.   /* Set up `MAKEFLAGS' again for the normal targets.  */
  1182.   define_makeflags (1, 0);
  1183.  
  1184.   {
  1185.     int status;
  1186.  
  1187.     /* If there were no command-line goals, use the default.  */
  1188.     if (goals == 0)
  1189.       {
  1190.     if (default_goal_file != 0)
  1191.       {
  1192.         goals = (struct dep *) xmalloc (sizeof (struct dep));
  1193.         goals->next = 0;
  1194.         goals->name = 0;
  1195.         goals->file = default_goal_file;
  1196.       }
  1197.       }
  1198.     else
  1199.       lastgoal->next = 0;
  1200.  
  1201.     if (goals != 0)
  1202.       {
  1203.     /* Update the goals.  */
  1204.  
  1205.     if (debug_flag)
  1206.       puts ("Updating goal targets....");
  1207. #ifdef DEBUG
  1208.         gettimeofday(&end_time, &time_zone);
  1209.         end_time.tv_sec -= start_time.tv_sec;
  1210.         end_time.tv_usec -= start_time.tv_usec;
  1211.         fprintf(stderr,"       [%d]: *** Total Initialization in %3d.%01d (seconds real).\n",
  1212.                 getpid(), end_time.tv_sec,
  1213.                 (int) (end_time.tv_usec/100000));
  1214.         fflush(stderr);
  1215. #endif
  1216.         
  1217.     switch (update_goal_chain (goals, 0))
  1218.       {
  1219.       case -1:
  1220.         /* Nothing happened.  */
  1221.       case 0:
  1222.         /* Updated successfully.  */
  1223.         status = 0;
  1224.         break;
  1225.       case 2:
  1226.         /* Updating failed.  */
  1227.         status = 2;
  1228.         break;
  1229.       case 1:
  1230.         /* We are under -q and would run some commands.  */
  1231.         status = 1;
  1232.         break;
  1233.       default:
  1234.         abort ();
  1235.       }
  1236.       }
  1237.     else
  1238.       {
  1239.     if (read_makefiles == 0)
  1240.       fatal ("No targets specified and no makefile found");
  1241.     else
  1242.       fatal ("No targets");
  1243.       }
  1244.  
  1245.     /* Exit.  */
  1246.     die (status);
  1247.   }
  1248.  
  1249.   return 0;
  1250. }
  1251.  
  1252. /* Parsing of arguments, decoding of switches.  */
  1253.  
  1254. static char options[1 + sizeof (switches) / sizeof (switches[0]) * 3];
  1255. static struct option long_options[(sizeof (switches) / sizeof (switches[0])) +
  1256.                   (sizeof (long_option_aliases) /
  1257.                    sizeof (long_option_aliases[0]))];
  1258.  
  1259. /* Fill in the string and vector for getopt.  */
  1260. static void
  1261. init_switches ()
  1262. {
  1263.   register char *p;
  1264.   register int c;
  1265.   register unsigned int i;
  1266.  
  1267.   if (options[0] != '\0')
  1268.     /* Already done.  */
  1269.     return;
  1270.  
  1271.   p = options;
  1272.  
  1273.   /* Return switch and non-switch args in order, regardless of
  1274.      POSIXLY_CORRECT.  Non-switch args are returned as option 1.  */
  1275.   *p++ = '-';
  1276.  
  1277.   for (i = 0; switches[i].c != '\0'; ++i)
  1278.     {
  1279.       long_options[i].name = (switches[i].long_name == 0 ? "" :
  1280.                   switches[i].long_name);
  1281.       long_options[i].flag = 0;
  1282.       long_options[i].val = switches[i].c;
  1283.       if (isalnum (switches[i].c))
  1284.     *p++ = switches[i].c;
  1285.       switch (switches[i].type)
  1286.     {
  1287.     case flag:
  1288.     case flag_off:
  1289.     case ignore:
  1290.       long_options[i].has_arg = no_argument;
  1291.       break;
  1292.  
  1293.     case string:
  1294.     case positive_int:
  1295.     case floating:
  1296.       if (isalnum (switches[i].c))
  1297.         *p++ = ':';
  1298.       if (switches[i].noarg_value != 0)
  1299.         {
  1300.           if (isalnum (switches[i].c))
  1301.         *p++ = ':';
  1302.           long_options[i].has_arg = optional_argument;
  1303.         }
  1304.       else
  1305.         long_options[i].has_arg = required_argument;
  1306.       break;
  1307.     }
  1308.     }
  1309.   *p = '\0';
  1310.   for (c = 0; c < (sizeof (long_option_aliases) /
  1311.            sizeof (long_option_aliases[0]));
  1312.        ++c)
  1313.     long_options[i++] = long_option_aliases[c];
  1314.   long_options[i].name = 0;
  1315. }
  1316.  
  1317. static void
  1318. handle_non_switch_argument (arg, env)
  1319.      char *arg;
  1320.      int env;
  1321. {
  1322.   /* Non-option argument.  It might be a variable definition.  */
  1323.   struct variable *v;
  1324.   if (arg[0] == '-' && arg[1] == '\0')
  1325.     /* Ignore plain `-' for compatibility.  */
  1326.     return;
  1327.   v = try_variable_definition ((char *) 0, 0, arg, o_command);
  1328.   if (v != 0)
  1329.     {
  1330.       /* It is indeed a variable definition.  Record a pointer to
  1331.      the variable for later use in define_makeflags.  */
  1332.       struct command_variable *cv
  1333.     = (struct command_variable *) xmalloc (sizeof (*cv));
  1334.       cv->variable = v;
  1335.       cv->next = command_variables;
  1336.       command_variables = cv;
  1337.     }
  1338.   else if (! env)
  1339.     {
  1340.       /* Not an option or variable definition; it must be a goal
  1341.      target!  Enter it as a file and add it to the dep chain of
  1342.      goals.  */
  1343.       struct file *f = enter_command_line_file (arg);
  1344.       f->cmd_target = 1;
  1345.       
  1346.       if (goals == 0)
  1347.     {
  1348.       goals = (struct dep *) xmalloc (sizeof (struct dep));
  1349.       lastgoal = goals;
  1350.     }
  1351.       else
  1352.     {
  1353.       lastgoal->next
  1354.         = (struct dep *) xmalloc (sizeof (struct dep));
  1355.       lastgoal = lastgoal->next;
  1356.     }
  1357.       lastgoal->name = 0;
  1358.       lastgoal->file = f;
  1359.     }
  1360. }
  1361.  
  1362. /* Decode switches from ARGC and ARGV.
  1363.    They came from the environment if ENV is nonzero.  */
  1364.  
  1365. static void
  1366. decode_switches (argc, argv, env)
  1367.      int argc;
  1368.      char **argv;
  1369.      int env;
  1370. {
  1371.   int bad = 0;
  1372.   register const struct command_switch *cs;
  1373.   register struct stringlist *sl;
  1374.   register int c;
  1375.  
  1376.   /* getopt does most of the parsing for us.
  1377.      First, get its vectors set up.  */
  1378.  
  1379.   init_switches ();
  1380.  
  1381.   /* Let getopt produce error messages for the command line,
  1382.      but not for options from the environment.  */
  1383.   opterr = !env;
  1384.   /* Reset getopt's state.  */
  1385.   optind = 0;
  1386.  
  1387.   while (optind < argc)
  1388.     {
  1389.       /* Parse the next argument.  */
  1390.       c = getopt_long (argc, argv, options, long_options, (int *) 0);
  1391.       if (c == EOF)
  1392.     /* End of arguments, or "--" marker seen.  */
  1393.     break;
  1394.       else if (c == 1)
  1395.     /* An argument not starting with a dash.  */
  1396.     handle_non_switch_argument (optarg, env);
  1397.       else if (c == '?')
  1398.     /* Bad option.  We will print a usage message and die later.
  1399.        But continue to parse the other options so the user can
  1400.        see all he did wrong.  */
  1401.     bad = 1;
  1402.       else
  1403.     for (cs = switches; cs->c != '\0'; ++cs)
  1404.       if (cs->c == c)
  1405.         {
  1406.           /* Whether or not we will actually do anything with
  1407.          this switch.  We test this individually inside the
  1408.          switch below rather than just once outside it, so that
  1409.          options which are to be ignored still consume args.  */
  1410.           int doit = !env || cs->env;
  1411.  
  1412.           switch (cs->type)
  1413.         {
  1414.         default:
  1415.           abort ();
  1416.  
  1417.         case ignore:
  1418.           break;
  1419.  
  1420.         case flag:
  1421.         case flag_off:
  1422.           if (doit)
  1423.             *(int *) cs->value_ptr = cs->type == flag;
  1424.           break;
  1425.  
  1426.         case string:
  1427.           if (!doit)
  1428.             break;
  1429.  
  1430.           if (optarg == 0)
  1431.             optarg = cs->noarg_value;
  1432.  
  1433.           sl = *(struct stringlist **) cs->value_ptr;
  1434.           if (sl == 0)
  1435.             {
  1436.               sl = (struct stringlist *)
  1437.             xmalloc (sizeof (struct stringlist));
  1438.               sl->max = 5;
  1439.               sl->idx = 0;
  1440.               sl->list = (char **) xmalloc (5 * sizeof (char *));
  1441.               *(struct stringlist **) cs->value_ptr = sl;
  1442.             }
  1443.           else if (sl->idx == sl->max - 1)
  1444.             {
  1445.               sl->max += 5;
  1446.               sl->list = (char **)
  1447.             xrealloc ((char *) sl->list,
  1448.                   sl->max * sizeof (char *));
  1449.             }
  1450.           sl->list[sl->idx++] = optarg;
  1451.           sl->list[sl->idx] = 0;
  1452.           break;
  1453.  
  1454.         case positive_int:
  1455.           if (optarg == 0 && argc > optind
  1456.               && isdigit (argv[optind][0]))
  1457.             optarg = argv[optind++];
  1458.  
  1459.           if (!doit)
  1460.             break;
  1461.  
  1462.           if (optarg != 0)
  1463.             {
  1464.               int i = atoi (optarg);
  1465.               if (i < 1)
  1466.             {
  1467.               if (doit)
  1468.                 error ("the `-%c' option requires a \
  1469. positive integral argument",
  1470.                    cs->c);
  1471.               bad = 1;
  1472.             }
  1473.               else
  1474.             *(unsigned int *) cs->value_ptr = i;
  1475.             }
  1476.           else
  1477.             *(unsigned int *) cs->value_ptr
  1478.               = *(unsigned int *) cs->noarg_value;
  1479.           break;
  1480.  
  1481.         case floating:
  1482.           if (optarg == 0 && optind < argc
  1483.               && (isdigit (argv[optind][0]) || argv[optind][0] == '.'))
  1484.             optarg = argv[optind++];
  1485.  
  1486.           if (doit)
  1487.             *(double *) cs->value_ptr            
  1488.               = (optarg != 0 ? atof (optarg)
  1489.              : *(double *) cs->noarg_value);
  1490.  
  1491.           break;
  1492.         }
  1493.         
  1494.           /* We've found the switch.  Stop looking.  */
  1495.           break;
  1496.         }
  1497.     }
  1498.  
  1499.   /* There are no more options according to getting getopt, but there may
  1500.      be some arguments left.  Since we have asked for non-option arguments
  1501.      to be returned in order, this only happens when there is a "--"
  1502.      argument to prevent later arguments from being options.  */
  1503.   while (optind < argc)
  1504.     handle_non_switch_argument (argv[optind++], env);
  1505.  
  1506.  
  1507.   if (!env && (bad || print_usage_flag))
  1508.     {
  1509.       /* Print a nice usage message.  */
  1510.       FILE *usageto;
  1511.  
  1512.       if (print_version_flag)
  1513.     print_version ();
  1514.  
  1515.       usageto = bad ? stderr : stdout;
  1516.  
  1517.       fprintf (usageto, "Usage: %s [options] [target] ...\n", program);
  1518.  
  1519.       fputs ("Options:\n", usageto);
  1520.       for (cs = switches; cs->c != '\0'; ++cs)
  1521.     {
  1522.       char buf[1024], shortarg[50], longarg[50], *p;
  1523.  
  1524.       if (cs->description[0] == '-')
  1525.         continue;
  1526.  
  1527.       switch (long_options[cs - switches].has_arg)
  1528.         {
  1529.         case no_argument:
  1530.           shortarg[0] = longarg[0] = '\0';
  1531.           break;
  1532.         case required_argument:
  1533.           sprintf (longarg, "=%s", cs->argdesc);
  1534.           sprintf (shortarg, " %s", cs->argdesc);
  1535.           break;
  1536.         case optional_argument:
  1537.           sprintf (longarg, "[=%s]", cs->argdesc);
  1538.           sprintf (shortarg, " [%s]", cs->argdesc);
  1539.           break;
  1540.         }
  1541.  
  1542.       p = buf;
  1543.  
  1544.       if (isalnum (cs->c))
  1545.         {
  1546.           sprintf (buf, "  -%c%s", cs->c, shortarg);
  1547.           p += strlen (p);
  1548.         }
  1549.       if (cs->long_name != 0)
  1550.         {
  1551.           unsigned int i;
  1552.           sprintf (p, "%s--%s%s",
  1553.                !isalnum (cs->c) ? "  " : ", ",
  1554.                cs->long_name, longarg);
  1555.           p += strlen (p);
  1556.           for (i = 0; i < (sizeof (long_option_aliases) /
  1557.                    sizeof (long_option_aliases[0]));
  1558.            ++i)
  1559.         if (long_option_aliases[i].val == cs->c)
  1560.           {
  1561.             sprintf (p, ", --%s%s",
  1562.                  long_option_aliases[i].name, longarg);
  1563.             p += strlen (p);
  1564.           }
  1565.         }
  1566.       {
  1567.         const struct command_switch *ncs = cs;
  1568.         while ((++ncs)->c != '\0')
  1569.           if (ncs->description[0] == '-' &&
  1570.           ncs->description[1] == cs->c)
  1571.         {
  1572.           /* This is another switch that does the same
  1573.              one as the one we are processing.  We want
  1574.              to list them all together on one line.  */
  1575.           sprintf (p, ", -%c%s", ncs->c, shortarg);
  1576.           p += strlen (p);
  1577.           if (ncs->long_name != 0)
  1578.             {
  1579.               sprintf (p, ", --%s%s", ncs->long_name, longarg);
  1580.               p += strlen (p);
  1581.             }
  1582.         }
  1583.       }
  1584.  
  1585.       if (p - buf > DESCRIPTION_COLUMN - 2)
  1586.         /* The list of option names is too long to fit on the same
  1587.            line with the description, leaving at least two spaces.
  1588.            Print it on its own line instead.  */
  1589.         {
  1590.           fprintf (usageto, "%s\n", buf);
  1591.           buf[0] = '\0';
  1592.         }
  1593.  
  1594.       fprintf (usageto, "%*s%s.\n",
  1595.            - DESCRIPTION_COLUMN,
  1596.            buf, cs->description);
  1597.     }
  1598.  
  1599.       die (bad ? 2 : 0);
  1600.     }
  1601. }
  1602.  
  1603. /* Decode switches from environment variable ENVAR (which is LEN chars long).
  1604.    We do this by chopping the value into a vector of words, prepending a
  1605.    dash to the first word if it lacks one, and passing the vector to
  1606.    decode_switches.  */
  1607.  
  1608. static void
  1609. decode_env_switches (envar, len)
  1610.      char *envar;
  1611.      unsigned int len;
  1612. {
  1613.   char *varref = (char *) alloca (2 + len + 2);
  1614.   char *value, *p;
  1615.   int argc;
  1616.   char **argv;
  1617.  
  1618.   /* Get the variable's value.  */
  1619.   varref[0] = '$';
  1620.   varref[1] = '(';
  1621.   bcopy (envar, &varref[2], len);
  1622.   varref[2 + len] = ')';
  1623.   varref[2 + len + 1] = '\0';
  1624.   value = variable_expand (varref);
  1625.  
  1626.   /* Skip whitespace, and check for an empty value.  */
  1627.   value = next_token (value);
  1628.   len = strlen (value);
  1629.   if (len == 0)
  1630.     return;
  1631.  
  1632.   /* Allocate a vector that is definitely big enough.  */
  1633.   argv = (char **) alloca ((1 + len + 1) * sizeof (char *));
  1634.  
  1635.   /* Allocate a buffer to copy the value into while we split it into words
  1636.      and unquote it.  We must use permanent storage for this because
  1637.      decode_switches may store pointers into the passed argument words.  */
  1638.   p = (char *) xmalloc (2 * len);
  1639.  
  1640.   /* getopt will look at the arguments starting at ARGV[1].
  1641.      Prepend a spacer word.  */
  1642.   argv[0] = 0;
  1643.   argc = 1;
  1644.   argv[argc] = p;
  1645.   while (*value != '\0')
  1646.     {
  1647.       if (*value == '\\')
  1648.     ++value;        /* Skip the backslash.  */
  1649.       else if (isblank (*value))
  1650.     {
  1651.       /* End of the word.  */
  1652.       *p++ = '\0';
  1653.       argv[++argc] = p;
  1654.       do
  1655.         ++value;
  1656.       while (isblank (*value));
  1657.       continue;
  1658.     }
  1659.       *p++ = *value++;
  1660.     }
  1661.   *p = '\0';
  1662.   argv[++argc] = 0;
  1663.  
  1664.   if (argc == 2 && argv[1][0] != '-')
  1665.     {
  1666.       /* There is just one word in the value, and it is not a switch.
  1667.      Either this is the single-word form and we should prepend a dash
  1668.      before calling decode_switches, or this is the multi-word form and
  1669.      there is no dash because it is a variable definition.  */
  1670.       struct variable *v;
  1671.       v = try_variable_definition ((char *) 0, 0, argv[1], o_command);
  1672.       if (v != 0)
  1673.     {
  1674.       /* It was indeed a variable definition, and now it has been
  1675.          processed.  There is nothing for decode_switches to do.
  1676.          Record a pointer to the variable for later use in
  1677.          define_makeflags.  */
  1678.       struct command_variable *cv
  1679.         = (struct command_variable *) xmalloc (sizeof (*cv));
  1680.       cv->variable = v;
  1681.       cv->next = command_variables;
  1682.       command_variables = cv;
  1683.       return;
  1684.     }
  1685.  
  1686.       /* It wasn't a variable definition, so it's some switches without a
  1687.      leading dash.  Add one and pass it along to decode_switches.  We
  1688.      need permanent storage for this in case decode_switches saves
  1689.      pointers into the value.  */
  1690.       argv[1] = concat ("-", argv[1], "");
  1691.     }
  1692.  
  1693.   /* Parse those words.  */
  1694.   decode_switches (argc, argv, 1);
  1695. }
  1696.  
  1697. /* Quote the string IN so that it will be interpreted as a single word with
  1698.    no magic by the shell; if DOUBLE_DOLLARS is nonzero, also double dollar
  1699.    signs to avoid variable expansion in make itself.  Write the result into
  1700.    OUT, returning the address of the next character to be written.
  1701.    Allocating space for OUT twice the length of IN (thrice if
  1702.    DOUBLE_DOLLARS is nonzero) is always sufficient.  */
  1703.  
  1704. static char *
  1705. quote_as_word (out, in, double_dollars)
  1706.      char *out, *in;
  1707.      int double_dollars;
  1708. {
  1709.   while (*in != '\0')
  1710.     {
  1711.       if (index ("^;'\"*?[]$<>(){}|&~`\\ \t\r\n\f\v", *in) != 0)
  1712.     *out++ = '\\';
  1713.       if (double_dollars && *in == '$')
  1714.     *out++ = '$';
  1715.       *out++ = *in++;
  1716.     }
  1717.  
  1718.   return out;
  1719. }
  1720.  
  1721. /* Define the MAKEFLAGS and MFLAGS variables to reflect the settings of the
  1722.    command switches.  Include options with args if ALL is nonzero.
  1723.    Don't include options with the `no_makefile' flag set if MAKEFILE.  */
  1724.  
  1725. static void
  1726. define_makeflags (all, makefile)
  1727.      int all, makefile;
  1728. {
  1729.   static const char ref[] = "$(MAKEOVERRIDES)";
  1730.   static const char posixref[] = "$(-*-command-variables-*-)";
  1731.   register const struct command_switch *cs;
  1732.   char *flagstring;
  1733.   register char *p;
  1734.   unsigned int words;
  1735.   struct variable *v;
  1736.  
  1737.   /* We will construct a linked list of `struct flag's describing
  1738.      all the flags which need to go in MAKEFLAGS.  Then, once we
  1739.      know how many there are and their lengths, we can put them all
  1740.      together in a string.  */
  1741.  
  1742.   struct flag
  1743.     {
  1744.       struct flag *next;
  1745.       const struct command_switch *cs;
  1746.       char *arg;
  1747.     };
  1748.   struct flag *flags = 0;
  1749.   unsigned int flagslen = 0;
  1750. #if NeXT || NeXT_PDO
  1751.   const char * end_of_make_flags = NULL;  
  1752. #endif
  1753.   
  1754. #define    ADD_FLAG(ARG, LEN) \
  1755.   do {                                          \
  1756.     struct flag *new = (struct flag *) alloca (sizeof (struct flag));          \
  1757.     new->cs = cs;                                  \
  1758.     new->arg = (ARG);                                  \
  1759.     new->next = flags;                                  \
  1760.     flags = new;                                  \
  1761.     if (new->arg == 0)                                  \
  1762.       ++flagslen;        /* Just a single flag letter.  */          \
  1763.     else                                      \
  1764.       flagslen += 1 + 1 + 1 + 1 + 3 * (LEN); /* " -x foo" */              \
  1765.     if (!isalnum (cs->c))                              \
  1766.       /* This switch has no single-letter version, so we use the long.  */    \
  1767.       flagslen += 2 + strlen (cs->long_name);                      \
  1768.   } while (0)
  1769.  
  1770.   for (cs = switches; cs->c != '\0'; ++cs)
  1771.     if (cs->toenv && (!makefile || !cs->no_makefile))
  1772.       switch (cs->type)
  1773.     {
  1774.     default:
  1775.       abort ();
  1776.  
  1777.     case ignore:
  1778.       break;
  1779.  
  1780.     case flag:
  1781.     case flag_off:
  1782.       if (!*(int *) cs->value_ptr == (cs->type == flag_off)
  1783.           && (cs->default_value == 0
  1784.           || *(int *) cs->value_ptr != *(int *) cs->default_value))
  1785.         ADD_FLAG (0, 0);
  1786.       break;
  1787.  
  1788.     case positive_int:
  1789.       if (all)
  1790.         {
  1791.           if ((cs->default_value != 0
  1792.            && (*(unsigned int *) cs->value_ptr
  1793.                == *(unsigned int *) cs->default_value)))
  1794.         break;
  1795.           else if (cs->noarg_value != 0
  1796.                && (*(unsigned int *) cs->value_ptr ==
  1797.                *(unsigned int *) cs->noarg_value))
  1798.         ADD_FLAG ("", 0); /* Optional value omitted; see below.  */
  1799.           else if (cs->c == 'j')
  1800.         /* Special case for `-j'.  */
  1801.         ADD_FLAG ("1", 1);
  1802.           else
  1803.         {
  1804.           char *buf = (char *) alloca (30);
  1805.           sprintf (buf, "%u", *(unsigned int *) cs->value_ptr);
  1806.           ADD_FLAG (buf, strlen (buf));
  1807.         }
  1808.         }
  1809.       break;
  1810.  
  1811.     case floating:
  1812.       if (all)
  1813.         {
  1814.           if (cs->default_value != 0
  1815.           && (*(double *) cs->value_ptr
  1816.               == *(double *) cs->default_value))
  1817.         break;
  1818.           else if (cs->noarg_value != 0
  1819.                && (*(double *) cs->value_ptr
  1820.                == *(double *) cs->noarg_value))
  1821.         ADD_FLAG ("", 0); /* Optional value omitted; see below.  */
  1822.           else
  1823.         {
  1824.           char *buf = (char *) alloca (100);
  1825.           sprintf (buf, "%g", *(double *) cs->value_ptr);
  1826.           ADD_FLAG (buf, strlen (buf));
  1827.         }
  1828.         }
  1829.       break;
  1830.  
  1831.     case string:
  1832.       if (all)
  1833.         {
  1834.           struct stringlist *sl = *(struct stringlist **) cs->value_ptr;
  1835.           if (sl != 0)
  1836.         {
  1837.           /* Add the elements in reverse order, because
  1838.              all the flags get reversed below; and the order
  1839.              matters for some switches (like -I).  */
  1840.           register unsigned int i = sl->idx;
  1841.           while (i-- > 0)
  1842.             ADD_FLAG (sl->list[i], strlen (sl->list[i]));
  1843.         }
  1844.         }
  1845.       break;
  1846.     }
  1847.  
  1848.   flagslen += 4 + sizeof posixref; /* Four more for the possible " -- ".  */
  1849.  
  1850. #undef    ADD_FLAG
  1851.  
  1852.   /* Construct the value in FLAGSTRING.
  1853.      We allocate enough space for a preceding dash and trailing null.  */
  1854.   flagstring = (char *) alloca (1 + flagslen + 1);
  1855.   p = flagstring;
  1856.   words = 1;
  1857.   *p++ = '-';
  1858.   while (flags != 0)
  1859.     {
  1860.       /* Add the flag letter or name to the string.  */
  1861.       if (!isalnum (flags->cs->c))
  1862.     {
  1863.       *p++ = '-';
  1864.       strcpy (p, flags->cs->long_name);
  1865.       p += strlen (p);
  1866.     }
  1867.       else
  1868.     *p++ = flags->cs->c;
  1869.       if (flags->arg != 0)
  1870.     {
  1871.       /* A flag that takes an optional argument which in this case is
  1872.          omitted is specified by ARG being "".  We must distinguish
  1873.          because a following flag appended without an intervening " -"
  1874.          is considered the arg for the first.  */
  1875.       if (flags->arg[0] != '\0')
  1876.         {
  1877.           /* Add its argument too.  */
  1878.           *p++ = !isalnum (flags->cs->c) ? '=' : ' ';
  1879.           p = quote_as_word (p, flags->arg, 1);
  1880.         }
  1881.       ++words;
  1882.       /* Write a following space and dash, for the next flag.  */
  1883.       *p++ = ' ';
  1884.       *p++ = '-';
  1885.     }
  1886.       else if (!isalnum (flags->cs->c))
  1887.     {
  1888.       ++words;
  1889.       /* Long options must each go in their own word,
  1890.          so we write the following space and dash.  */
  1891.       *p++ = ' ';
  1892.       *p++ = '-';
  1893.     }
  1894.       flags = flags->next;
  1895.     }
  1896.  
  1897.   if (all && command_variables != 0)
  1898.     {
  1899.       /* Now write a reference to $(MAKEOVERRIDES), which contains all the
  1900.      command-line variable definitions.  */
  1901.  
  1902.       if (p == &flagstring[1])
  1903. #if NeXT || NeXT_PDO
  1904.         {
  1905.          /* No flags written, so elide the leading dash already written.  */
  1906.          p = flagstring;
  1907.          end_of_make_flags = flagstring;
  1908.         }
  1909.       else
  1910.     {
  1911.          end_of_make_flags = p;
  1912. #else
  1913.          /* No flags written, so elide the leading dash already written.  */
  1914.         p = flagstring;
  1915.       else
  1916.         {
  1917. #endif         
  1918.           /* Separate the variables from the switches with a "--" arg.  */
  1919.       if (p[-1] != '-')
  1920.         {
  1921.           /* We did not already write a trailing " -".  */
  1922.           *p++ = ' ';
  1923.           *p++ = '-';
  1924.         }
  1925.       /* There is a trailing " -"; fill it out to " -- ".  */
  1926.       *p++ = '-';
  1927.       *p++ = ' ';
  1928.     }
  1929.  
  1930.       /* Copy in the string.  */
  1931.       if (posix_pedantic)
  1932.     {
  1933.       bcopy (posixref, p, sizeof posixref - 1);
  1934.       p += sizeof posixref - 1;
  1935.     }
  1936.       else
  1937.     {
  1938.       bcopy (ref, p, sizeof ref - 1);
  1939.       p += sizeof ref - 1;
  1940.     }
  1941.     }
  1942.   else if (p == &flagstring[1])
  1943.     {
  1944.       words = 0;
  1945.       --p;
  1946.     }
  1947.   else if (p[-1] == '-')
  1948.     /* Kill the final space and dash.  */
  1949.     p -= 2;
  1950.   /* Terminate the string.  */
  1951.   *p = '\0';
  1952.  
  1953.   v = define_variable ("MAKEFLAGS", 9,
  1954.                /* If there is just a single word of switches,
  1955.               omit the leading dash unless it is a single
  1956.               long option with two leading dashes.  */
  1957.                &flagstring[(words == 1 && command_variables == 0
  1958.                     && flagstring[1] != '-')
  1959.                    ? 1 : 0],
  1960.                /* This used to use o_env, but that lost when a
  1961.               makefile defined MAKEFLAGS.  Makefiles set
  1962.               MAKEFLAGS to add switches, but we still want
  1963.               to redefine its value with the full set of
  1964.               switches.  Of course, an override or command
  1965.               definition will still take precedence.  */
  1966.                o_file, 1);
  1967.   if (! all)
  1968.     /* The first time we are called, set MAKEFLAGS to always be exported.
  1969.        We should not do this again on the second call, because that is
  1970.        after reading makefiles which might have done `unexport MAKEFLAGS'. */
  1971.     v->export = v_export;
  1972.  
  1973. #if NeXT || NeXT_PDO
  1974.   if (end_of_make_flags)
  1975.      *end_of_make_flags = '\0';
  1976. #else
  1977.   /* Since MFLAGS is not parsed for flags, there is no reason to
  1978.      override any makefile redefinition.  */
  1979. #endif  
  1980.   (void) define_variable ("MFLAGS", 6, flagstring, o_env, 1);
  1981. #if 0
  1982.   fprintf(stderr,"gnumake: defining MFLAGS to be: %s\n",flagstring);
  1983. #endif
  1984. }
  1985.  
  1986. /* Print version information.  */
  1987.  
  1988. static void
  1989. print_version ()
  1990. {
  1991.   static int printed_version = 0;
  1992.  
  1993.   char *precede = print_data_base_flag ? "# " : "";
  1994.  
  1995.   if (printed_version)
  1996.     /* Do it only once.  */
  1997.     return;
  1998.  
  1999.   printf ("%sGNU Make version %s", precede, version_string);
  2000.   if (remote_description != 0 && *remote_description != '\0')
  2001.     printf ("-%s", remote_description);
  2002.  
  2003.   printf (", by Richard Stallman and Roland McGrath.\n\
  2004. %sCopyright (C) 1988, 89, 90, 91, 92, 93, 94, 95 Free Software Foundation, Inc.\n\
  2005. %sThis is free software; see the source for copying conditions.\n\
  2006. %sThere is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\n\
  2007. %sPARTICULAR PURPOSE.\n\n", precede, precede, precede, precede);
  2008.  
  2009.   printed_version = 1;
  2010.  
  2011.   /* Flush stdout so the user doesn't have to wait to see the
  2012.      version information while things are thought about.  */
  2013.   fflush (stdout);
  2014. }
  2015.  
  2016. /* Print a bunch of information about this and that.  */
  2017.  
  2018. static void
  2019. print_data_base ()
  2020. {
  2021.   extern char *ctime ();
  2022.   time_t when;
  2023.  
  2024.   when = time ((time_t *) 0);
  2025.   printf ("\n# Make data base, printed on %s", ctime (&when));
  2026.  
  2027.   print_variable_data_base ();
  2028.   print_dir_data_base ();
  2029.   print_rule_data_base ();
  2030.   print_file_data_base ();
  2031.   print_vpath_data_base ();
  2032.  
  2033.   when = time ((time_t *) 0);
  2034.   printf ("\n# Finished Make data base on %s\n", ctime (&when));
  2035. }
  2036.  
  2037. /* Exit with STATUS, cleaning up as necessary.  */
  2038.  
  2039. void
  2040. die (status)
  2041.      int status;
  2042. {
  2043.   static char dying = 0;
  2044.  
  2045.   if (!dying)
  2046.     {
  2047.       int err;
  2048.  
  2049.       dying = 1;
  2050.  
  2051.       /* Try to move back to the original directory.  This is essential on
  2052.      MS-DOS (where there is really only one process), and on Unix it
  2053.      puts core files in the original directory instead of the -C
  2054.      directory.  */
  2055.       if (directory_before_chdir != 0)
  2056.     chdir (directory_before_chdir);
  2057.  
  2058.       if (print_version_flag)
  2059.     print_version ();
  2060.  
  2061.       /* Wait for children to die.  */
  2062.       for (err = status != 0; job_slots_used > 0; err = 0)
  2063.     reap_children (1, err);
  2064.  
  2065.       /* Remove the intermediate files.  */
  2066.       remove_intermediates (0);
  2067.  
  2068.       if (print_data_base_flag)
  2069.     print_data_base ();
  2070.  
  2071.       if (print_directory_flag)
  2072.     log_working_directory (0);
  2073.     }
  2074.  
  2075.   exit (status);
  2076. }
  2077.  
  2078. /* Write a message indicating that we've just entered or
  2079.    left (according to ENTERING) the current directory.  */
  2080.  
  2081. static void
  2082. log_working_directory (entering)
  2083.      int entering;
  2084. {
  2085.   static int entered = 0;
  2086.   char *message = entering ? "Entering" : "Leaving";
  2087.  
  2088.   if (entering)
  2089.     entered = 1;
  2090.   else if (!entered)
  2091.     /* Don't print the leaving message if we
  2092.        haven't printed the entering message.  */
  2093.     return;
  2094.  
  2095.   if (print_data_base_flag)
  2096.     fputs ("# ", stdout);
  2097.  
  2098.   if (makelevel == 0)
  2099.     printf ("%s: %s ", program, message);
  2100.   else
  2101.     printf ("%s[%u]: %s ", program, makelevel, message);
  2102.  
  2103.   if (starting_directory == 0)
  2104.     puts ("an unknown directory");
  2105.   else
  2106.     printf ("directory `%s'\n", starting_directory);
  2107. }
  2108.